import cv2
import numpy as np
from matplotlib import pyplot as plt

image = cv2.imread('../IMAGES/football.jpg',0)
img = image.copy()
rows, cols = img.shape
print(img.shape)

template = cv2.imread('../IMAGES/football_ball.jpg',0)
h, w = template.shape
print(template.shape)

img_norm = img/255
template_norm = template/255

img_norm = img_norm - np.mean(img_norm)
template_norm = template_norm - np.mean(template_norm)

filter_res = cv2.filter2D(img_norm, -1, template_norm)

cv2.normalize(filter_res,filter_res,0,255,cv2.NORM_MINMAX)
conv = np.uint8(filter_res)
c_h, c_w = np.unravel_index(np.argmax(filter_res), filter_res.shape)
print(c_h, c_w)

cv2.rectangle(image,(c_w-20, c_h-20),(c_w+20, c_h+20),(255,255,255),2)

plt.subplot(121)
plt.imshow(image,cmap = 'gray')
plt.subplot(122)
plt.imshow(filter_res,cmap = 'gray')
plt.show()
